package com.jeesite.modules.mo.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.ic.entity.IcUser;
import com.jeesite.modules.ic.service.IcUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.mo.entity.MoTixian;
import com.jeesite.modules.mo.service.MoTixianService;
import org.springframework.web.client.RestTemplate;

/**
 * 用户提现Controller
 * @author 李寒冰
 * @version 2024-10-23
 */
@Controller
@RequestMapping(value = "${adminPath}/mo/moTixian")
public class MoTixianController extends BaseController {

	@Autowired
	private MoTixianService moTixianService;
    @Autowired
    private IcUserService icUserService;

	@Value("${pic.reqUrl}")
	private String reqUrl;

	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public MoTixian get(String id, boolean isNewRecord) {
		return moTixianService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("mo:moTixian:view")
	@RequestMapping(value = {"list", ""})
	public String list(MoTixian moTixian, Model model) {
		model.addAttribute("moTixian", moTixian);
		model.addAttribute("reqUrl", reqUrl);
		return "modules/mo/moTixianList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("mo:moTixian:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<MoTixian> listData(MoTixian moTixian, HttpServletRequest request, HttpServletResponse response) {

		if(StringUtils.isNotEmpty(moTixian.getMobile())){
			String mobile = moTixian.getMobile();
			IcUser byMobile = icUserService.getByMobile(mobile);
			if(byMobile!=null){
				moTixian.setUserId(byMobile.getId());
			}
		}

		moTixian.setPage(new Page<>(request, response));
		if(StringUtils.isNotEmpty(request.getParameter("workDate_gte"))) {
			moTixian.setWorkDateStart(request.getParameter("workDate_gte"));
		}
		if(StringUtils.isNotEmpty(request.getParameter("workDate_lte"))) {
			moTixian.setWorkDateEnd(request.getParameter("workDate_lte")+" 23:59:59");
		}
		Page<MoTixian> page = moTixianService.findPage(moTixian);
		for (MoTixian moTixian1 : page.getList()) {
			String userId = moTixian1.getUserId();
			IcUser icUser = icUserService.get(userId);
			moTixian1.setIcUser(icUser);
		}
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("mo:moTixian:view")
	@RequestMapping(value = "form")
	public String form(MoTixian moTixian, Model model) {
		model.addAttribute("moTixian", moTixian);
		return "modules/mo/moTixianForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("mo:moTixian:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated MoTixian moTixian) {
		moTixianService.save(moTixian);
		return renderResult(Global.TRUE, text("保存用户提现成功！"));
	}

	/**
	 * 提现审核通过
	 */
	@RequiresPermissions("mo:moTixian:edit")
	@PostMapping(value = "tongguoTixian")
	@ResponseBody
	public String tongguoTixian( MoTixian moTixian) {
		String result = restTemplate.getForObject(reqUrl + "/api/money/tongguoTixian?id=" + moTixian.getId(), String.class);
		JSONObject jsonObject = JSONObject.parseObject(result);
		if(jsonObject.getString("result").equals("true")){
			return renderResult(Global.TRUE, text("通过成功！"));
		}else{
			return renderResult(Global.FALSE,jsonObject.getString("message"));
		}
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("mo:moTixian:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(MoTixian moTixian) {
		moTixianService.delete(moTixian);
		return renderResult(Global.TRUE, text("删除用户提现成功！"));
	}
	
}